
#include "stdafx.h"
#include "AppWinMFC.h"
#include "WinMFCSystem.h"
#include "MainFrm.h"
#include "MFCSubClasses.h"
#include "memory"
float BrushValueTable[25] = {1,2,4,6,8,10,12,15,18,21,25,30,35,40,50,60,70,80,90,100,110,120,130,140,150};
static int HomeLargeImageCount = 0;
static int HomeSmallImageCount = 0;


void CMainFrame::CreateCameraMenu()
{
    m_CamBtn = new CMFCRibbonButton(ID_CAMERA_SAVE,_T("Camera"),0);
	m_CamBtn->SetDefaultCommand(0);
    for(int i = 0;i < 10;i++)
	{
        CMFCRibbonButton *pSub = new CMFCRibbonButton(ID_CAMERA_0 + i,_T("(Empty)"),0);	
	    pSub->AddSubItem(new CMFCRibbonButton(ID_CAMERA_0 + i,_T("Select"),-1),0);
	    pSub->AddSubItem(new CMFCRibbonButton(ID_CAMERA_REMOVE_0 + i,_T("Delete"),-1),1);
	    m_CamBtn->AddSubItem(pSub,i);
	}
}
//------------------------------------------------------------------------------------
void CMainFrame::CreateHomeCategory()
{
	CString strTemp;
  
    // Add "Home" category with "Clipboard" panel:
	BOOL bNameValid = strTemp.LoadString(IDS_RIBBON_HOME);
	ASSERT(bNameValid);
	CMFCRibbonCategory* pCategoryHome = m_wndRibbonBar.AddCategory(strTemp, IDB_WRITESMALL, IDB_WRITELARGE);
    m_CatHome = pCategoryHome;

	HomeSmallImageCount = pCategoryHome->GetSmallImages().GetCount();
	HomeLargeImageCount = pCategoryHome->GetLargeImages().GetCount();

	CMFCToolBarImages smallImages;
	smallImages.Load(_T("..\\Data\\.cache\\SmallIcons.bmp"));
	for(int idx = 0;idx < smallImages.GetCount();idx++)
	{
	    pCategoryHome->GetSmallImages().AddImage(smallImages,idx);
	}

	CMFCToolBarImages largeImages;
	SIZE iconsz;
	iconsz.cx = 32;
	iconsz.cy = 32;
	largeImages.SetImageSize(iconsz);
	largeImages.Load(_T("..\\Data\\.cache\\LargeIcons.bmp"));
	for(int idx = 0;idx < largeImages.GetCount();idx++)
	{
	    pCategoryHome->GetLargeImages().AddImage(largeImages,idx);
	}

	int c1 = pCategoryHome->GetLargeImages().GetCount();
	int c2 = pCategoryHome->GetSmallImages().GetCount();

	CMFCRibbonPanel* pProject = pCategoryHome->AddPanel(_T("Project"), m_PanelImages.ExtractIcon (7));
	pProject->Add(new CMFCRibbonButton(ID_FILE_NEW,_T("New Scene"),-1,15));
	pProject->Add(new CMFCRibbonButton(ID_FILE_OPEN,_T("Open"),-1,16));
	pProject->Add(new CMFCRibbonButton(ID_FILE_SAVE,_T("Save"),-1,17));
	pProject->Add(new CMFCRibbonButton(ID_FILE_SAVE_AS,_T("Save As"),-1,18));
	pProject->Add(new CMFCRibbonButton(ID_OPTIONSDLG,_T("Scene Settings"),-1,19));
	CMFCRibbonButton *ImportBtn = new CMFCRibbonButton(ID_FILE_IMPORT,_T("Import"),-1,3);
	StringVector theList;
	unsigned int valID = ID_IMPORT_START;
	CMFCRibbonButton *tmpBtn;
	theApp.m_OgitorsRoot->GetImportSerializerList(theList);
	
	for(unsigned int l = 0;l < theList.size();l++)
	{
	    tmpBtn = new CMFCRibbonButton(valID + l,CString(theList[l].c_str()),-1,-1);
	    ImportBtn->AddSubItem(tmpBtn,l);
	}

	CMFCRibbonButton *ExportBtn = new CMFCRibbonButton(ID_FILE_EXPORT,_T("Export"),-1,4);
    valID = ID_EXPORT_START;

	theApp.m_OgitorsRoot->GetExportSerializerList(theList);
	
	for(unsigned int l = 0;l < theList.size();l++)
	{
	    tmpBtn = new CMFCRibbonButton(valID + l,CString(theList[l].c_str()),-1,-1);
	    ExportBtn->AddSubItem(tmpBtn,l);
	}

	pProject->Add(ImportBtn);
	pProject->Add(ExportBtn);

	// Create and add a "View" panel:
	bNameValid = strTemp.LoadString(IDS_RIBBON_VIEW);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelView = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

	bNameValid = strTemp.LoadString(IDS_RIBBON_WINDOWS);
	ASSERT(bNameValid);
	CMFCRibbonButton *pWindowsBtn = new CMFCRibbonButton(ID_VIEW_WINDOWS, strTemp, 0, 1);
	pWindowsBtn->SetMenu(IDR_VIEW_MENU, FALSE /* No default command */, TRUE /* Right align */);
	pPanelView->Add(pWindowsBtn);

	pPanelView->Add(new CMFCRibbonButton(ID_OGITOR_UNDO,_T("Undo"),10,-1));
	pPanelView->Add(new CMFCRibbonButton(ID_OGITOR_REDO,_T("Redo"),26,-1));

	// Create and add a "Tools" panel:
	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelTools = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SELECT);
	ASSERT(bNameValid);
	pPanelTools->Add(new CMFCRibbonButton(ID_TOOLS_SELECT, strTemp, 11, 2));
	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_MOVE);
	ASSERT(bNameValid);
	pPanelTools->Add(new CMFCRibbonButton(ID_TOOLS_MOVE, strTemp, 12));
	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_ROTATE);
	ASSERT(bNameValid);
	pPanelTools->Add(new CMFCRibbonButton(ID_TOOLS_ROTATE, strTemp, 13));
	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SCALE);
	ASSERT(bNameValid);
	pPanelTools->Add(new CMFCRibbonButton(ID_TOOLS_SCALE, strTemp, 14));
	
	pPanelTools->Add(new CMFCRibbonCheckBox(ID_TOOLS_AXISX, _T("X Axis")));
	pPanelTools->Add(new CMFCRibbonCheckBox(ID_TOOLS_AXISY, _T("Y Axis")));
	pPanelTools->Add(new CMFCRibbonCheckBox(ID_TOOLS_AXISZ, _T("Z Axis")));


	bNameValid = strTemp.LoadString(IDS_RIBBON_OBJECTS);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelObjects = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

	CMFCRibbonButton *objectBtn = new CMFCRibbonButton(ID_OBJECTS_START + 3, _T("Entity"), -1, HomeLargeImageCount + 3);
    pPanelObjects->Add(objectBtn);
    
    StringVector objectList = theApp.m_OgitorsRoot->GetSortedTypeList();
    for(unsigned ob = 4;ob < objectList.size();ob++)
    {
	    OGITORREGISTRATIONSTRUCT *data = theApp.m_OgitorsRoot->GetTypeData(objectList[ob]);
	    if(!data->AddToObjectList) 
            continue;
	    int smallIDX = -1;
	    if(data->Icon16 != "") 
            smallIDX = data->TypeID + HomeSmallImageCount;
	    unsigned int CommandID = ID_OBJECTS_START + data->TypeID;
	    CString btnstring(data->TypeName.c_str());
	    if(btnstring.Find(_T(" Object")) > 0)
		    btnstring.Delete(btnstring.Find(_T(" Object")),7);
	    CMFCRibbonButton *objectBtn = new CMFCRibbonButton(CommandID, btnstring, smallIDX, -1);
        pPanelObjects->Add(objectBtn);
  	}

	CMFCRibbonPanel* pCameraOptions = pCategoryHome->AddPanel(_T("Camera Options"), m_PanelImages.ExtractIcon (7));


	pCameraOptions->Add(new CMFCRibbonButton(ID_CAMERA_SAVE,_T("Save Position"),25,13));
	CMFCRibbonButton *pCamMode = new CMFCRibbonButton(0,_T("Polygon Mode"),25,14);
	pCamMode->AddSubItem(new CMFCRibbonButton(ID_CAMERA_MODE_SOLID,_T("Solid"),15),0);
    pCamMode->AddSubItem(new CMFCRibbonButton(ID_CAMERA_MODE_WIREFRAME,_T("Wireframe"),16),1);
	pCamMode->AddSubItem(new CMFCRibbonButton(ID_CAMERA_MODE_POINT,_T("Point"),17),2);
	pCamMode->SetDefaultCommand(0);
	pCameraOptions->Add(pCamMode);

    pCameraOptions->Add(new CMFCRibbonButton(ID_CAMERA_SPEED_INCREASE,_T("+Speed"),18));
	pCameraOptions->Add(new CMFCRibbonButton(ID_CAMERA_SPEED_DECREASE,_T("-Speed"),19));
}

	void 	CMainFrame::CreateMusicToolsCategory()
	{
		//CString strTemp = _T("Music Tools");
		//BOOL bNameValid = strTemp.LoadString(IDS_RIBBON_ETL);
		CString strTemp;
		BOOL bNameValid = strTemp.LoadString(IDS_RIBBON_MUSIC);
		ASSERT(bNameValid);

		CMFCRibbonCategory* pCategoryMusic = m_wndRibbonBar.AddCategory(strTemp, IDB_WRITESMALL, IDB_WRITELARGE);
		m_CatMusic = pCategoryMusic;

        //m_wndRibbonBar.SetActiveCategory(m_CatMusic);
 
		// ETL_TOOLS_PANEL
		bNameValid = strTemp.LoadString(IDS_RIBBON_MUSIC_TOOLS);
		ASSERT(bNameValid);
		CMFCRibbonPanel* pPanelETLTools = pCategoryMusic->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

 		bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_PLAY);
 		ASSERT(bNameValid);
		CMFCRibbonButton * but = new CMFCRibbonButton(ID_TOOLS_PLAY, strTemp, 11, 2);
			
	    pPanelETLTools->Add(but);


 		bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_PAUSE);
 		ASSERT(bNameValid);
 		pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_PAUSE, strTemp, 20, 5));
 
 		bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_STOP);
 		ASSERT(bNameValid);
 		pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_STOP, strTemp, 21, 6));


		bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SETMUSIC);
		ASSERT(bNameValid);
		pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_SETMUSIC, strTemp, 22, 7));

 


		CMFCRibbonPanel* pPanelETLBrush2 = pCategoryMusic->AddPanel(_T("Brush Options"), m_PanelImages.ExtractIcon (7));
		m_BrushSizeLabel = new CMFCRibbonLabel(_T("music slider"));
		m_MusicSizeLabel = m_BrushSizeLabel;
		pPanelETLBrush2->Add(m_BrushSizeLabel);
		pPanelETLBrush2->Add(new CMFCRibbonLabel(_T(" ")));
		m_BrushIntensityLabel = new CMFCRibbonLabel(_T("music slider"));
		pPanelETLBrush2->Add(m_BrushIntensityLabel);
		CMFCRibbonSlider* pBrushSize = new CMFCRibbonSlider(ID_MUSIC_BRUSHSIZE,400);
		m_MusicLabel = pBrushSize;
		pBrushSize->SetRange(0,100);
		pBrushSize->SetPos(0,1);
		pBrushSize->SetZoomIncrement(1);
		pBrushSize->SetZoomButtons(true);
		pBrushSize->SetToolTipText(_T("music"));

		pPanelETLBrush2->Add(pBrushSize);
		pPanelETLBrush2->Add(new CMFCRibbonLabel(_T(" ")));
 
 		CMFCRibbonSlider* pBrushIntensity = new CMFCRibbonSlider(ID_MUSIC_BRUSHSIZE,400);
 		pBrushIntensity->SetRange(0,100);
 		pBrushIntensity->SetPos(0,1);
 		pBrushIntensity->SetZoomIncrement(1);
 		pBrushIntensity->SetZoomButtons(true);
 		pBrushIntensity->SetToolTipText(_T("music"));
 		pPanelETLBrush2->Add(pBrushIntensity);










	}

//----------------------------------------------------------------------------------
void CMainFrame::CreateETLToolsCategory()
{
    // Create the ETL Tools Category
	CString strTemp;
    BOOL bNameValid = strTemp.LoadString(IDS_RIBBON_ETL);
	ASSERT(bNameValid);
	CMFCRibbonCategory* pCategoryETL = m_wndRibbonBar.AddCategory(strTemp, IDB_WRITESMALL, IDB_WRITELARGE);
	m_CatETL = pCategoryETL;
	// ETL_TOOLS_PANEL
	bNameValid = strTemp.LoadString(IDS_RIBBON_ETL_TOOLS);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelETLTools = pCategoryETL->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SELECT);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_SELECT, strTemp, 11, 2));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_DEFORM);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_DEFORM, strTemp, 20, 5));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SPLAT);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_SPLAT, strTemp, 21, 6));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_PAINT);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_PAINT, strTemp, 22, 7));

	bNameValid = strTemp.LoadString(IDS_RIBBON_TOOLS_SMOOTH);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_TOOLS_SMOOTH, strTemp, 24, 8));

	bNameValid = strTemp.LoadString(IDS_RIBBON_ETL_CALCLIGHT);
	ASSERT(bNameValid);
	pPanelETLTools->Add(new CMFCRibbonButton(ID_ETL_CALCLIGHT, strTemp, 16, 12));

	// ETL_BRUSH_PANEL
	bNameValid = strTemp.LoadString(IDS_RIBBON_ETL_BRUSH);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelETLBrush = pCategoryETL->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));
	
	m_BrushGallery = new CGalleryEx(ID_ETL_BRUSHES, _T("Brushes\nk"), 0, 11, m_BrushImages);
	m_BrushGallery->SetIconsInRow(4);
	m_BrushGallery->EnableMenuResize(TRUE, TRUE);
	pPanelETLBrush->Add(m_BrushGallery);

    CMFCRibbonPanel* pPanelETLBrush2 = pCategoryETL->AddPanel(_T("Brush Options"), m_PanelImages.ExtractIcon (7));
    m_BrushSizeLabel = new CMFCRibbonLabel(_T("Size ( 12 )"));
    pPanelETLBrush2->Add(m_BrushSizeLabel);
	pPanelETLBrush2->Add(new CMFCRibbonLabel(_T(" ")));
	m_BrushIntensityLabel = new CMFCRibbonLabel(_T("Intensity ( 12 )"));
    pPanelETLBrush2->Add(m_BrushIntensityLabel);
	CMFCRibbonSlider* pBrushSize = new CMFCRibbonSlider(ID_ETL_BRUSHSIZE,48);
	pBrushSize->SetRange(0,24);
	pBrushSize->SetPos(7,1);
	pBrushSize->SetZoomIncrement(1);
    pBrushSize->SetZoomButtons(true);
	pBrushSize->SetToolTipText(_T("12"));
	pPanelETLBrush2->Add(pBrushSize);
	pPanelETLBrush2->Add(new CMFCRibbonLabel(_T(" ")));
	CMFCRibbonSlider* pBrushIntensity = new CMFCRibbonSlider(ID_ETL_BRUSHINTENSITY,48);
	pBrushIntensity->SetRange(0,24);
	pBrushIntensity->SetPos(7,1);
	pBrushIntensity->SetZoomIncrement(1);
    pBrushIntensity->SetZoomButtons(true);
	pBrushIntensity->SetToolTipText(_T("12"));
	pPanelETLBrush2->Add(pBrushIntensity);

	
	// ETL_COLOR_PANEL
	bNameValid = strTemp.LoadString(IDS_RIBBON_ETL_COLOR);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelETLColor = pCategoryETL->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

    std::auto_ptr<CMFCRibbonColorButton> apBtnFillColor(new CMFCRibbonColorButton(ID_ETL_COLORS, _T("Paint Color\nsf"), FALSE, -1, 9));
	apBtnFillColor->SetDefaultCommand(FALSE);
	apBtnFillColor->EnableOtherButton(_T("&More Colors..."), _T("More Colors"));
	apBtnFillColor->SetColumns(10);
	apBtnFillColor->SetColorBoxSize(CSize(17, 17));
	apBtnFillColor->AddColorsGroup(_T("Theme Colors"), m_lstMainColors);
	apBtnFillColor->AddColorsGroup(_T(""), m_lstAdditionalColors, TRUE);
	apBtnFillColor->AddColorsGroup(_T("Standard Colors"), m_lstStandardColors);
	apBtnFillColor->SetColor((COLORREF)-1);
	pPanelETLColor->Add(apBtnFillColor.release());


	// ETL_TEXTURE_PANEL
	bNameValid = strTemp.LoadString(IDS_RIBBON_ETL_TEXTURE);
	ASSERT(bNameValid);
	CMFCRibbonPanel* pPanelETLTexture = pCategoryETL->AddPanel(strTemp, m_PanelImages.ExtractIcon (7));

	m_TTextureGallery = new CGalleryEx(ID_ETL_TEXTURES, _T("Textures\nk"), -1, 10, m_TextureImages);
	m_TTextureGallery->SetIconsInRow(4);
	m_TTextureGallery->EnableMenuResize(TRUE, TRUE);
	pPanelETLTexture->Add(m_TTextureGallery);
    
	unsigned int i;
    CString tooltip;
	for(i = 0;i < m_BrushNames.size();i++)
	{
	    tooltip = m_BrushNames[i].c_str();
	    m_BrushGallery->SetItemToolTip(i,tooltip);
	}
	for(i = 0;i < m_TTextureNames.size();i++)
	{
	    tooltip = m_TTextureNames[i].c_str();
	    m_TTextureGallery->SetItemToolTip(i,tooltip);
	}
}
//------------------------------------------------------------------------------------
stringvector tmpimglist16;
stringvector tmpimglist32;

void CMainFrame::AddItemToButton(CMFCRibbonButton *parent, OGUIWidget *item)
{
    int index16 = -1;
    if(item->GetBitmap(16) != "")
    {
        tmpimglist16.push_back(item->GetBitmap(16));
        index16 = tmpimglist16.size();
    }

    if(item->GetType() == WIDGET_MENU)
    {

        CMFCRibbonButton *button = new CMFCRibbonButton(item->GetSystemID(),CString(item->GetCaption().c_str()),index16,-1);          
        item->SetHandle((void*)button);
        WidgetVector children = item->GetChildren();
        for(unsigned int i = 0;i < children.size();i++)
        {
            AddItemToButton(button, children[i]);
        }
        parent->AddSubItem(button);
    }
    else if(item->GetType() == WIDGET_MENUITEM)
    {
        CMFCRibbonButton *button = new CMFCRibbonButton(item->GetSystemID(),CString(item->GetCaption().c_str()),index16,-1);          
        item->SetHandle((void*)button);
        parent->AddSubItem(button);
    }
}
//------------------------------------------------------------------------------------
void CMainFrame::AddItemToToolbar(CMFCRibbonPanel *panel, OGUIWidget *item, bool subtoolbar)
{
    switch(item->GetType())
    {
    case WIDGET_TOOLBAR:
    case WIDGET_SUBTOOLBAR:
        {
            item->SetHandle((void *)panel);
            WidgetVector children = item->GetChildren();
            for(unsigned int it = 0;it < children.size();it++)
            {
                AddItemToToolbar(panel, children[it], true);
            }
            break;
        }
    case WIDGET_SEPARATOR:panel->AddSeparator();break;
    case WIDGET_LABEL:
        {
            CMFCRibbonLabel *label = new CMFCRibbonLabel(CString(item->GetCaption().c_str()));
            item->SetHandle((void*)label);
            panel->Add(label);
            break;
        }
    case WIDGET_COMBO:
        {
            OGUICombo *widget = static_cast<OGUICombo*>(item);
            CMFCRibbonComboBox *combo = new CMFCRibbonComboBox(widget->GetSystemID(),0,widget->GetWidth() - 17);          
            stringvector itemlist = widget->GetItems();
            for(unsigned int i = 0;i < itemlist.size();i++)
            {
                combo->AddItem(CString(itemlist[i].c_str()));
            }
            combo->SelectItem(widget->GetIndex());
            widget->SetHandle((void*)combo);
            panel->Add(combo);
            break;
        }
    case WIDGET_EDIT:
        {
            OGUIEdit *widget = static_cast<OGUIEdit*>(item);
			CMFCRibbonEdit *edit;
			if(widget->GetHideChars())
				edit = new CMFCRibbonEditPassword(widget->GetSystemID(),widget->GetWidth(),CString(widget->GetCaption().c_str()));
			else
				edit = new CMFCRibbonEdit(widget->GetSystemID(),widget->GetWidth(),CString(widget->GetCaption().c_str()));
            edit->SetEditText(CString(widget->GetText().c_str()));
            widget->SetHandle((void*)edit);
            panel->Add(edit);
            break;
        }
    case WIDGET_BUTTON:
        {
            int index16 = -1;
            int index32 = -1;
            
            if(item->GetBitmap(32) != "" && !subtoolbar)
            {
                tmpimglist32.push_back(item->GetBitmap(32));
                index32 = tmpimglist32.size();
            }

            if(item->GetBitmap(16) != "")
            {
                tmpimglist16.push_back(item->GetBitmap(16));
                index16 = tmpimglist16.size();
            }

            if(index16 == -1 && index32 == -1)
                index32 = 0;

            CMFCRibbonButton *button = new CMFCRibbonButton(item->GetSystemID(),CString(item->GetCaption().c_str()), index16, index32);          
            item->SetHandle((void*)button);
            OGUIButton *widget = static_cast<OGUIButton*>(item);
            WidgetVector children = widget->GetChildren();
            for(unsigned int i = 0;i < children.size();i++)
            {
                WidgetVector menuchildren = children[i]->GetChildren();
                for(unsigned int it = 0;it < menuchildren.size();it++)
                {
                    AddItemToButton(button, menuchildren[it]);
                }
            }
            panel->Add(button);
            break;
        }
    }
}
//------------------------------------------------------------------------------------
void CMainFrame::CreateCustomCategories()
{
    CategoryBarVector categories = ClientRoot::getInstance().GetCategoryBars();
    
    for(unsigned int i = 0;i < categories.size();i++)
    {
        OGUICategoryBar *category = categories[i];
        CMFCRibbonCategory* pCategory = m_wndRibbonBar.AddCategory(CString(category->GetCaption().c_str()), IDB_CUSTOMSMALL, IDB_CUSTOMLARGE);
        WidgetVector toolbars = category->GetChildren();
        categories[i]->SetHandle((void *)pCategory);

        tmpimglist16.clear();
        tmpimglist32.clear();

        for(unsigned int it = 0;it < toolbars.size();it++)
        {
            if(toolbars[it]->GetType() != WIDGET_TOOLBAR)
                continue;

            CMFCRibbonPanel* pPanel = pCategory->AddPanel(CString(toolbars[it]->GetCaption().c_str()), m_PanelImages.ExtractIcon (7));
            toolbars[it]->SetHandle((void*)pPanel);

            WidgetVector children = toolbars[it]->GetChildren();
            for(unsigned int ch = 0;ch < children.size();ch++)
            {
                AddItemToToolbar(pPanel, children[ch], false);
            }
        }

        if((tmpimglist16.size() > 0) || (tmpimglist32.size() > 0))
        {
            CreateToolbarImageList(tmpimglist16, tmpimglist32);

	        CMFCToolBarImages largeImages;
            CMFCToolBarImages smallImages;
	        SIZE iconsz;
	        iconsz.cx = 32;
	        iconsz.cy = 32;
	        largeImages.SetImageSize(iconsz);
	        iconsz.cx = 16;
	        iconsz.cy = 16;
	        smallImages.SetImageSize(iconsz);

            largeImages.Load(_T("..\\Data\\.cache\\toolbarlarge.bmp"));
	        for(int idx = 0;idx < largeImages.GetCount();idx++)
	        {
	            pCategory->GetLargeImages().AddImage(largeImages,idx);
	        }

	        smallImages.Load(_T("..\\Data\\.cache\\toolbarsmall.bmp"));
	        for(int idx = 0;idx < smallImages.GetCount();idx++)
	        {
	            pCategory->GetSmallImages().AddImage(smallImages,idx);
	        }
        }
    }
}
//------------------------------------------------------------------------------------
void CMainFrame::CreateDocumentColors()
{
	typedef struct
	{
		COLORREF color;
		TCHAR* szName;
	}
	ColorTableEntry;

	int i = 0;
	int nNumColours = 0;

	static ColorTableEntry colorsMain [] =
	{
		{ RGB(255, 255, 255), _T("White, Background 1") },
		{ RGB(0, 0, 0), _T("Black, Text 1") },
		{ RGB(238, 236, 225), _T("Tan, Background 2") },
		{ RGB(31, 73, 125), _T("Dark Blue, Text 2") },
		{ RGB(79, 129, 189), _T("Blue, Accent 1") },
		{ RGB(192, 80, 77), _T("Red, Accent 2") },
		{ RGB(155, 187, 89), _T("Olive Green, Accent 3") },
		{ RGB(128, 100, 162), _T("Purple, Accent 4") },
		{ RGB(75, 172, 198), _T("Aqua, Accent 5") },
		{ RGB(245, 150, 70), _T("Orange, Accent 6") }
	};

	static ColorTableEntry colorsAdditional [] =
	{
		{ RGB(242, 242, 242), _T("White, Background 1, Darker 5%") },
		{ RGB(127, 127, 127), _T("Black, Text 1, Lighter 50%") },
		{ RGB(214, 212, 202), _T("Tan, Background 2, Darker 10%") },
		{ RGB(210, 218, 229), _T("Dark Blue, Text 2, Lighter 80%") },
		{ RGB(217, 228, 240), _T("Blue, Accent 1, Lighter 80%") },
		{ RGB(244, 219, 218), _T("Red, Accent 2, Lighter 80%") },
		{ RGB(234, 241, 221), _T("Olive Green, Accent 3, Lighter 80%")},
		{ RGB(229, 223, 235), _T("Purple, Accent 4, Lighter 80%") },
		{ RGB(216, 237, 242), _T("Aqua, Accent 5, Lighter 80%") },
		{ RGB(255, 234, 218), _T("Orange, Accent 6, Lighter 80%") },
		{ RGB(215, 215, 215), _T("White, Background 1, Darker 15%") },
		{ RGB(89, 89, 89), _T("Black, Text 1, Lighter 35%") },
		{ RGB(177, 176, 167), _T("Tan, Background 2, Darker 25%") },
		{ RGB(161, 180, 201), _T("Dark Blue, Text 2, Lighter 60%") },
		{ RGB(179, 202, 226), _T("Blue, Accent 1, Lighter 60%") },
		{ RGB(233, 184, 182), _T("Red, Accent 2, Lighter 60%") },
		{ RGB(213, 226, 188), _T("Olive Green, Accent 3, Lighter 60%")},
		{ RGB(203, 191, 215), _T("Purple, Accent 4, Lighter 60%") },
		{ RGB(176, 220, 231), _T("Aqua, Accent 5, Lighter 60%") },
		{ RGB(255, 212, 181), _T("Orange, Accent 6, Lighter 60%") },
		{ RGB(190, 190, 190), _T("White, Background 1, Darker 25%") },
		{ RGB(65, 65, 65), _T("Black, Text 1, Lighter 25%") },
		{ RGB(118, 117, 112), _T("Tan, Background 2, Darker 35%") },
		{ RGB(115, 143, 175), _T("Dark Blue, Text 2, Lighter 40%") },
		{ RGB(143, 177, 213), _T("Blue, Accent 1, Lighter 40%") },
		{ RGB(222, 149, 147), _T("Red, Accent 2, Lighter 40%") },
		{ RGB(192, 213, 155), _T("Olive Green, Accent 3, Lighter 40%")},
		{ RGB(177, 160, 197), _T("Purple, Accent 4, Lighter 40%") },
		{ RGB(137, 203, 218), _T("Aqua, Accent 5, Lighter 40%") },
		{ RGB(255, 191, 145), _T("Orange, Accent 6, Lighter 40%") },
		{ RGB(163, 163, 163), _T("White, Background 1, Darker 35%") },
		{ RGB(42, 42, 42), _T("Black, Text 1, Lighter 15%") },
		{ RGB(61, 61, 59), _T("Tan, Background 2, Darker 50%") },
		{ RGB(20, 57, 92), _T("Dark Blue, Text 2, Darker 25%") },
		{ RGB(54, 96, 139), _T("Blue, Accent 1, Darker 25%") },
		{ RGB(149, 63, 60), _T("Red, Accent 2, Darker 25%") },
		{ RGB(114, 139, 71), _T("Olive Green, Accent 3, Darker 25%") },
		{ RGB(97, 76, 119), _T("Purple, Accent 4, Darker 25%") },
		{ RGB(41, 128, 146), _T("Aqua, Accent 5, Darker 25%") },
		{ RGB(190, 112, 59), _T("Orange, Accent 6, Darker 25%") },
		{ RGB(126, 126, 126), _T("White, Background 1, Darker 50%") },
		{ RGB(20, 20, 20), _T("Black, Text 1, Lighter 5%") },
		{ RGB(29, 29, 28), _T("Tan, Background 2, Darker 90%") },
		{ RGB(17, 40, 64), _T("Dark Blue, Text 2, Darker 50%") },
		{ RGB(38, 66, 94), _T("Blue, Accent 1, Darker 50%") },
		{ RGB(100, 44, 43), _T("Red, Accent 2, Darker 50%") },
		{ RGB(77, 93, 49), _T("Olive Green, Accent 3, Darker 50%") },
		{ RGB(67, 53, 81), _T("Purple, Accent 4, Darker 50%") },
		{ RGB(31, 86, 99), _T("Aqua, Accent 5, Darker 50%") },
		{ RGB(126, 77, 42), _T("Orange, Accent 6, Darker 50%") },
	};

	static ColorTableEntry colorsStandard [] =
	{
		{ RGB(200, 15, 18), _T("Dark Red") },
		{ RGB(255, 20, 24), _T("Red") },
		{ RGB(255, 191, 40), _T("Orange") },
		{ RGB(255, 255, 49), _T("Yellow") },
		{ RGB(138, 207, 87), _T("Light Green") },
		{ RGB(0, 175, 84), _T("Green") },
		{ RGB(0, 174, 238), _T("Light Blue") },
		{ RGB(0, 111, 189), _T("Blue") },
		{ RGB(0, 36, 95), _T("Black") },
		{ RGB(114, 50, 157), _T("Purple") },
	};

	m_lstMainColors.RemoveAll();
	nNumColours = sizeof(colorsMain) / sizeof(ColorTableEntry);

	for (i = 0; i < nNumColours; i++)
	{
		m_lstMainColors.AddTail(colorsMain [i].color);
		CMFCRibbonColorButton::SetColorName(colorsMain [i].color, colorsMain [i].szName);
	};

	m_lstAdditionalColors.RemoveAll();
	nNumColours = sizeof(colorsAdditional) / sizeof(ColorTableEntry);

	for (i = 0; i < nNumColours; i++)
	{
		m_lstAdditionalColors.AddTail(colorsAdditional [i].color);
		CMFCRibbonColorButton::SetColorName(colorsAdditional [i].color, colorsAdditional [i].szName);
	};

	m_lstStandardColors.RemoveAll();
	nNumColours = sizeof(colorsStandard) / sizeof(ColorTableEntry);

	for (i = 0; i < nNumColours; i++)
	{
		m_lstStandardColors.AddTail(colorsStandard [i].color);
		CMFCRibbonColorButton::SetColorName(colorsStandard [i].color, colorsStandard [i].szName);
	};
}
//------------------------------------------------------------------------------------
COLORREF CMainFrame::GetColorFromColorButton(int nButtonID)
{
	CArray<CMFCRibbonBaseElement*, CMFCRibbonBaseElement*> arButtons;

	m_wndRibbonBar.GetElementsByID(nButtonID, arButtons);

	if (arButtons.GetSize() == 0)
	{
		return(COLORREF) -1;
	}

	CMFCRibbonColorButton* pColorButton = (CMFCRibbonColorButton*)arButtons.GetAt(0);

	COLORREF clr = pColorButton->GetColor();
	if (clr == (COLORREF)-1)
	{
		clr = pColorButton->GetAutomaticColor();
	}

	return clr;
}
//------------------------------------------------------------------------------------
int CMainFrame::GetSliderState(int nButtonID)
{
	CArray<CMFCRibbonBaseElement*, CMFCRibbonBaseElement*> arButtons;

	m_wndRibbonBar.GetElementsByID(nButtonID, arButtons);

	if (arButtons.GetSize() == 0)
	{
		return 0.0f;
	}

	CMFCRibbonSlider* pSlider = (CMFCRibbonSlider*)arButtons.GetAt(0);
	CString tooltip;
	tooltip.Format(_T("%d"),BrushValueTable[pSlider->GetPos()]);
	pSlider->SetToolTipText(tooltip);
	return pSlider->GetPos();
}
//------------------------------------------------------------------------------------
void CMainFrame::UpdateStatusBarPane(int nID, CString strText)
{
	CMFCRibbonBaseElement* pPane = m_wndStatusBar.FindByID(nID);
	if (pPane != NULL)
	{
		pPane->SetText(strText);
		m_wndStatusBar.RecalcLayout();
		pPane->Redraw();
	}
}
//------------------------------------------------------------------------------------
void CMainFrame::SetActiveCategory(int idx)
{
 switch(idx)
 {
 case 0:m_wndRibbonBar.SetActiveCategory(m_CatHome);break;
 case 1:m_wndRibbonBar.SetActiveCategory(m_CatETL);break;

 }
}

//------------------------------------------------------------------------------------
//---------GDI++ SECTION-----------------------------------------------
//------------------------------------------------------------------------------------
#include <gdiplus.h>
using namespace Gdiplus;

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes

   ImageCodecInfo* pImageCodecInfo = NULL;

   GetImageEncodersSize(&num, &size);
   if(size == 0)
       return -1;  // Failure

   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
       return -1;  // Failure

   GetImageEncoders(num, size, pImageCodecInfo);

   for(UINT j = 0; j < num; ++j)
   {
       if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
       {
           *pClsid = pImageCodecInfo[j].Clsid;
           free(pImageCodecInfo);
           return j;  // Success
       }    
   }

   free(pImageCodecInfo);
   return -1;  // Failure
}
//------------------------------------------------------------------------------------
#define IMAGE_SIZE 48
//------------------------------------------------------------------------------------
void CMainFrame::CreateImageCaches()
{
//     GdiplusStartupInput gdiplusStartupInput;
//     ULONG_PTR           gdiplusToken;
//     CLSID clsPNG;
// 
//     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//     GetEncoderClsid(_T("image/bmp"),&clsPNG);
// 
//     m_BrushFiles.clear();
//     m_TerrainTextureFiles.clear();
//     m_BrushNames.clear();
//     m_TTextureNames.clear();
// 
//     FileInfoListPtr resList = ResourceGroupManager::getSingleton().listResourceFileInfo("Brushes");
//   
//     for (FileInfoList::const_iterator it = resList->begin(); it != resList->end(); ++it)
//     {
// 	    FileInfo fInfo = (*it);
// 	    if(fInfo.archive->getType() == "FileSystem")
// 	    {
// 	        String filename = fInfo.archive->getName() + "/";
// 	        filename += fInfo.filename;
// 	        m_BrushFiles.push_back(filename);
// 	        m_BrushNames.push_back(fInfo.filename);
// 	    }
//     }
//     resList.setNull();
// 
//     int iCount = m_BrushFiles.size();
//     if(iCount > 0)
//     {
//         Gdiplus::Bitmap *imgBrushes = new Bitmap(iCount * IMAGE_SIZE,IMAGE_SIZE,PixelFormat32bppARGB);
//         Gdiplus::Graphics mygr(imgBrushes);
//         Gdiplus::Rect rc(0,0,IMAGE_SIZE,IMAGE_SIZE);
//         for(int i = 0;i < iCount;i++)
//         {
// 	        CString filenm;
// 	        filenm = m_BrushFiles[i].c_str();
// 	        Gdiplus::Image img(filenm);
// 	        mygr.DrawImage(&img,rc);
// 	        rc.X += IMAGE_SIZE;
//         }
//         imgBrushes->Save(_T("..\\Data\\.cache\\Brushes.bmp"),&clsPNG);
//         delete imgBrushes;
//     }
// 
//     resList = ResourceGroupManager::getSingleton().listResourceFileInfo("TerrainTextures");
//   
//     for (FileInfoList::const_iterator it = resList->begin(); it != resList->end(); ++it)
//     {
// 	    FileInfo fInfo = (*it);
// 	    if(fInfo.archive->getType() == "FileSystem")
// 	    {
// 	        String filename = fInfo.archive->getName() + "/";
// 	        filename += fInfo.filename;
// 	        m_TerrainTextureFiles.push_back(filename);
//   	        m_TTextureNames.push_back(fInfo.filename);
// 	    }
//     }
//     resList.setNull();
// 
//     iCount = m_TerrainTextureFiles.size();
//     if(iCount > 0)
//     {
//         Gdiplus::Bitmap *imgTextures = new Bitmap(iCount * IMAGE_SIZE,IMAGE_SIZE,PixelFormat32bppARGB);
//         Gdiplus::Graphics mygr(imgTextures);
//         Gdiplus::Rect rc(0,0,IMAGE_SIZE,IMAGE_SIZE);
//         for(int i = 0;i < iCount;i++)
//         {
//             CString filenm;
//             filenm = m_TerrainTextureFiles[i].c_str();
//             Gdiplus::Image img(filenm);
//             mygr.DrawImage(&img,rc);
//             rc.X += IMAGE_SIZE;
//         }
//         imgTextures->Save(_T("..\\Data\\.cache\\TTextures.bmp"),&clsPNG);
//         delete imgTextures;
//     }
// 
//     StringVector objectList = theApp.m_OgitorsRoot->GetSortedTypeList();
// 
//     Gdiplus::Bitmap *smlIcons = new Bitmap(16 * objectList.size(),16,PixelFormat32bppARGB);
//     Gdiplus::Bitmap *lrgIcons = new Bitmap(32 * objectList.size(),32,PixelFormat32bppARGB);
//     Gdiplus::Graphics grSmall(smlIcons);
//     Gdiplus::Graphics grLarge(lrgIcons);
//     Gdiplus::Rect rcSmall(0,0,16,16);
//     Gdiplus::Rect rcLarge(32,0,32,32);
// 
//     grSmall.Clear(Gdiplus::Color(0,0,0,0));
//     grLarge.Clear(Gdiplus::Color(0,0,0,0));
// 
//     Gdiplus::Image projimg(_T("..\\Plugins\\Icons\\project.png"));
//     grSmall.DrawImage(&projimg,rcSmall);
//     rcSmall.X += 16;
//     for(unsigned ob = 1;ob < objectList.size();ob++)
//     {
//         CString filenm;
//         OGITORREGISTRATIONSTRUCT *data = theApp.m_OgitorsRoot->GetTypeData(objectList[ob]);
//         filenm = CString(data->Icon16.c_str());
//         if(filenm != "")
//         {
//             Gdiplus::Image img(_T("..\\Plugins\\") + filenm);
//             grSmall.DrawImage(&img,rcSmall);
//         }
//         rcSmall.X += 16;
// 
//         filenm = CString(data->Icon32.c_str());
//         if(filenm != "")
//         {
//             Gdiplus::Image img(_T("..\\Plugins\\") + filenm);
//             grLarge.DrawImage(&img,rcLarge);
//         }
//         rcLarge.X += 32;
//     }
//     smlIcons->Save(_T("..\\Data\\.cache\\SmallIcons.bmp"),&clsPNG);
//     lrgIcons->Save(_T("..\\Data\\.cache\\LargeIcons.bmp"),&clsPNG);
//     delete smlIcons;
//     delete lrgIcons;
// 
//     GdiplusShutdown(gdiplusToken);
}
//------------------------------------------------------------------------------------
void CMainFrame::CreateToolbarImageList(stringvector imglist16, stringvector imglist32)
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    CLSID clsPNG;

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    GetEncoderClsid(_T("image/bmp"),&clsPNG);

    Gdiplus::Bitmap *smlIcons = new Bitmap(16 * (imglist16.size() + 1),16,PixelFormat32bppARGB);
    Gdiplus::Bitmap *lrgIcons = new Bitmap(32 * (imglist32.size() + 1),32,PixelFormat32bppARGB);
    Gdiplus::Graphics grSmall(smlIcons);
    Gdiplus::Graphics grLarge(lrgIcons);
    Gdiplus::Rect rcSmall(0,0,16,16);
    Gdiplus::Rect rcLarge(0,0,32,32);

    grSmall.Clear(Gdiplus::Color(0,0,0,0));
    grLarge.Clear(Gdiplus::Color(0,0,0,0));

    for(unsigned ob = 0;ob < imglist16.size();ob++)
    {
        CString filenm;
        filenm = CString(imglist16[ob].c_str());
        if(filenm != "")
        {
            Gdiplus::Image img(_T("..\\Plugins\\") + filenm);
            grSmall.DrawImage(&img,rcSmall);
        }
        rcSmall.X += 16;
    }

    for(unsigned ob = 0;ob < imglist32.size();ob++)
    {
        CString filenm;
        filenm = CString(imglist32[ob].c_str());
        if(filenm != "")
        {
            Gdiplus::Image img(_T("..\\Plugins\\") + filenm);
            grLarge.DrawImage(&img,rcLarge);
        }
        rcLarge.X += 32;
    }

    DeleteFile(_T("..\\Data\\.cache\\toolbarsmall.bmp"));
    DeleteFile(_T("..\\Data\\.cache\\toolbarlarge.bmp"));
    smlIcons->Save(_T("..\\Data\\.cache\\toolbarsmall.bmp"),&clsPNG);
    lrgIcons->Save(_T("..\\Data\\.cache\\toolbarlarge.bmp"),&clsPNG);
    delete smlIcons;
    delete lrgIcons;

    GdiplusShutdown(gdiplusToken);
}
//------------------------------------------------------------------------------------

